<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<link rel="STYLESHEET" href="lib.css" type="text/css">
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png">
<link rel="start" href="../index.html" title="Python documentation Index">
<link rel="first" href="lib.html" title="Python library Reference">
<link rel="contents" href="contents.html" title="Contents">
<link rel="index" href="genindex.html" title="Index">
<link rel="last" href="about.html" title="About this document...">
<link rel="help" href="about.html" title="About this document...">
<link rel="next" href="module-glob.html">
<link rel="prev" href="module-filecmp.html">
<link rel="parent" href="filesys.html">
<link rel="next" href="module-glob.html">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="aesop" content="information">
<title>11.6 tempfile -- 生成临时文件和目录</title></head>

<body>
<div class="navigation">
<div id="top-navigation-panel" xml:id="top-navigation-panel">
<table align="center" cellpadding="0" cellspacing="2" width="100%">
<tbody><tr>
<td class="online-navigation"><a rel="prev" title="11.5.1 the dircmp class" href="dircmp-objects.html"><img src="../icons/previous.png" alt="Previous Page" border="0" height="32" width="32"></a></td>
<td class="online-navigation"><a rel="parent" title="11. file and Directory" href="filesys.html"><img src="../icons/up.png" alt="Up one Level" border="0" height="32" width="32"></a></td>
<td class="online-navigation"><a rel="next" title="11.7 glob  " href="module-glob.html"><img src="../icons/next.png" alt="Next Page" border="0" height="32" width="32"></a></td>
<td align="center" width="100%">Python库参考</td>
<td class="online-navigation"><a rel="contents" title="Table of Contents" href="contents.html"><img src="../icons/contents.png" alt="Contents" border="0" height="32" width="32"></a></td>
<td class="online-navigation"><a href="modindex.html" title="Module Index"><img src="../icons/modules.png" alt="Module Index" border="0" height="32" width="32"></a></td>
<td class="online-navigation"><a rel="index" title="Index" href="genindex.html"><img src="../icons/index.png" alt="Index" border="0" height="32" width="32"></a></td>
</tr></tbody></table>
<div class="online-navigation"><b class="navlabel">前一节：</b><a class="sectref" rel="prev" href="dircmp-objects.html">11.5.1 dircmp类</a><strong> 上一级：</strong><a class="sectref" rel="parent" href="filesys.html">11.文件和目录</a> <b class="navlabel">下一节：</b><a class="sectref" rel="next" href="module-glob.html">11.7 glob</a></div>
<hr></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0013600000000000000000">
11.6 <tt class="module">tempfile</tt> --
         生成临时文件和目录</a></h1>

<p>
<a name="module-tempfile"></a>

</p><p>
<a id="l2h-2267" xml:id="l2h-2267"></a><a id="l2h-2268" xml:id="l2h-2268"></a>
</p><p>
这个模块生成临时文件和目录。它工作在所有支持的平台上。
</p><p>
在Python 2.3 版本中，这个模块被用来检查增强安全性。现在它提供三个新的函数，<tt class="function">NamedTemporaryFile()</tt>，<tt class="function">mkstemp()</tt>，和
<tt class="function">mkdtemp()</tt>，应该取消所有仍然需要使用不安全的 <tt class="function">mktemp()</tt> 函数。通过这个模块创建的临时文件名不再包含进程 ID；替代使用由六个随机字符组成的一个字符串。
</p><p>
同样地，现在所有用户调用的函数通过附加参数允许直接控制临时文件的位置和名称。不再一定要使用全局的 <var>tempdir</var> 和
<var>template</var> 变量。对于维护向后的兼容性，参数的顺序多少有点奇怪；为了清晰性建议使用关键字参数。
</p><p>
该模块定义了下面的用户调用函数：
</p><p>
</p><dl><dt><table cellpadding="0" cellspacing="0"><tbody><tr valign="baseline">
  <td><nobr><b><tt id="l2h-2269" xml:id="l2h-2269" class="function">TemporaryFile</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>mode=<code>'w+b'</code></var><big>[</big><var>,
                                bufsize=<code>-1</code></var><big>[</big><var>,
                                suffix</var><big>[</big><var>, prefix</var><big>[</big><var>, dir</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></tbody></table></dt>
<dd>
返回可以被用作一个临时存储区的文件(或类文件)对象。该文件使用 <tt class="function">mkstemp</tt> 创建。它将一将被关闭就被破坏(包括该对象被垃圾收集时的一个隐式关闭)。在 <span class="Unix">Unix下</span>，文件被创建后该文件的目录项被立即移除。其他平台不支持这个特性；你的代码不应该依赖使用这个函数创建的在文件系统中有或没有一个可见的名字的临时文件。
<p><var>mode</var> 参数缺省为 <code>'w+b'</code> 以便于创建的文件不用被关闭就可以读写。使用二进制模式以便于它在所有平台上一致的行为而无需注意它的数据存储。<var>bufsize</var> 缺省为 <code>-1</code>，意思使用操作系统的缺省。
</p><p><var>dir</var>，<var>prefix</var> 和 <var>suffix</var> 参数被传递给 <tt class="function">mkstemp()</tt>。
</p></dd></dl>

<p>
</p><dl><dt><table cellpadding="0" cellspacing="0"><tbody><tr valign="baseline">
  <td><nobr><b><tt id="l2h-2270" xml:id="l2h-2270" class="function">NamedTemporaryFile</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>mode=<code>'w+b'</code></var><big>[</big><var>,
                                     bufsize=<code>-1</code></var><big>[</big><var>,
                                     suffix</var><big>[</big><var>, prefix</var><big>[</big><var>,
                                     dir</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></tbody></table></dt>
<dd>这个函数和 <tt class="function">TemporaryFile()</tt> 的操作一样，除了文件被约定在文件系统上有一个可见的名字 (<span class="Unix">在Unix 上</span>，目录项不被解链)。名称可以从文件对象的 <tt class="member">name</tt> 成员中被更改。当以这个名称命名的临时文件仍然被打开时，是否该文件名可以被再次用来打开文件，因平台而异(它可以用在 <span class="Unix">Unix</span> 平台上；但是不能用在 Windows NT 或更新的平台上)。<span class="versionnote">2.3 版本中的新特性。</span>

</dd></dl>

<p>
</p><dl><dt><table cellpadding="0" cellspacing="0"><tbody><tr valign="baseline">
  <td><nobr><b><tt id="l2h-2271" xml:id="l2h-2271" class="function">mkstemp</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>suffix</var><big>[</big><var>,
                          prefix</var><big>[</big><var>, dir</var><big>[</big><var>, text</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></tbody></table></dt>
<dd>用尽可能安全的方法创建一个临时文件。在文件的创建中没有类别条件，假设适当的平台为
<tt class="function">os.open()</tt>实现 <tt class="constant">O_EXCL</tt>  标记。该文件仅被创建者的 ID可写可读。如果平台用权限位标识一个文件是否可执行，该文件不被任何人可执行。该文件描述符不被子进程继承。

<p>与<tt class="function">TemporaryFile()</tt> 不同，<tt class="function">mkstemp()</tt> 的用户负责当完成它的时候删除临时文件。

</p><p>
If <var>suffix</var> is specified, the file name will end with that suffix,
otherwise there will be no suffix.  <tt class="function">mkstemp()</tt> does not put a
dot between the file name and the suffix; if you need one, put it at
the beginning of <var>suffix</var>.

</p><p>
If <var>prefix</var> is specified, the file name will begin with that
prefix; otherwise, a default prefix is used.

</p><p>
If <var>dir</var> is specified, the file will be created in that directory;
otherwise, a default directory is used.  The default directory is chosen
from a platform-dependent list, but the user of the application can control
the directory location by setting the <var>TMPDIR</var>, <var>TEMP</var> or <var>TMP</var>
environment variables.  There is thus no guarantee that the generated
filename will have any nice properties, such as not requiring quoting when
passed to external commands via <code>os.popen()</code>.

</p><p>
If <var>text</var> is specified, it indicates whether to open the file in
binary mode (the default) or text mode.  On some platforms, this makes
no difference.

</p><p>
<tt class="function">mkstemp()</tt> returns a tuple containing an OS-level handle to
an open file (as would be returned by <tt class="function">os.open()</tt>) and the
absolute pathname of that file, in that order.

<span class="versionnote">New in version 2.3.</span>

</p></dd></dl>

<p>
</p><dl><dt><table cellpadding="0" cellspacing="0"><tbody><tr valign="baseline">
  <td><nobr><b><tt id="l2h-2272" xml:id="l2h-2272" class="function">mkdtemp</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>suffix</var><big>[</big><var>, prefix</var><big>[</big><var>, dir</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></tbody></table></dt>
<dd>
Creates a temporary directory in the most secure manner possible.
There are no race conditions in the directory's creation.  The
directory is readable, writable, and searchable only by the
creating user ID.

<p>
The user of <tt class="function">mkdtemp()</tt> is responsible for deleting the
temporary directory and its contents when done with it.

</p><p>
The <var>prefix</var>, <var>suffix</var>, and <var>dir</var> arguments are the same
as for <tt class="function">mkstemp()</tt>.

</p><p>
<tt class="function">mkdtemp()</tt> returns the absolute pathname of the new directory.

<span class="versionnote">New in version 2.3.</span>

</p></dd></dl>

<p>
</p><dl><dt><table cellpadding="0" cellspacing="0"><tbody><tr valign="baseline">
  <td><nobr><b><tt id="l2h-2273" xml:id="l2h-2273" class="function">mktemp</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>suffix</var><big>[</big><var>, prefix</var><big>[</big><var>, dir</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></tbody></table></dt>
<dd>
<div class="versionnote"><b>2.3 版本后不推荐使用。代替使用</b> <tt class="function">mkstemp()</tt>。</div><p></p>
Return an absolute pathname of a file that did not exist at the time
the call is made.  The <var>prefix</var>, <var>suffix</var>, and <var>dir</var>
arguments are the same as for <tt class="function">mkstemp()</tt>.

<p>
<span class="warning"><b class="label">Warning:</b>
Use of this function may introduce a security hole in your
program.  By the time you get around to doing anything with the file
name it returns, someone else may have beaten you to the punch.</span>
</p></dd></dl>

<p>
The module uses two global variables that tell it how to construct a
temporary name.  They are initialized at the first call to any of the
functions above.  The caller may change them, but this is discouraged;
use the appropriate function arguments, instead.

</p><p>
</p><dl><dt><b><tt id="l2h-2274" xml:id="l2h-2274">tempdir</tt></b></dt>
<dd>
When set to a value other than <code>None</code>, this variable defines the
default value for the <var>dir</var> argument to all the functions defined
in this module.

<p>
If <code>tempdir</code> is unset or <code>None</code> at any call to any of the
above functions, Python searches a standard list of directories and
sets <var>tempdir</var> to the first one which the calling user can create
files in.  The list is:

</p><p>

</p><ol>
<li>The directory named by the <a class="envvar" id="l2h-2278" xml:id="l2h-2278">TMPDIR</a> environment variable.
</li>
<li>The directory named by the <a class="envvar" id="l2h-2279" xml:id="l2h-2279">TEMP</a> environment variable.
</li>
<li>The directory named by the <a class="envvar" id="l2h-2280" xml:id="l2h-2280">TMP</a> environment variable.
</li>
<li>A platform-specific location:
    
<ul>
<li>On RiscOS, the directory named by the
          <a class="envvar" id="l2h-2281" xml:id="l2h-2281">Wimp$ScrapDir</a> environment variable.
</li>
<li>On Windows, the directories
          <span class="file">C:<span class="MATH"><img src="img2.png" alt="$\backslash$" align="middle" border="0" height="32" width="12"></span>TEMP</span>,
          <span class="file">C:<span class="MATH"><img src="img2.png" alt="$\backslash$" align="middle" border="0" height="32" width="12"></span>TMP</span>,
          <span class="file"><span class="MATH"><img src="img2.png" alt="$\backslash$" align="middle" border="0" height="32" width="12"></span>TEMP</span>, and
          <span class="file"><span class="MATH"><img src="img2.png" alt="$\backslash$" align="middle" border="0" height="32" width="12"></span>TMP</span>, in that order.
</li>
<li>On all other platforms, the directories
          <span class="file">/tmp</span>, <span class="file">/var/tmp</span>, and <span class="file">/usr/tmp</span>, in that order.
    
</li>
</ul>
</li>
<li>As a last resort, the current working directory.
</li>
</ol>
</dd></dl>

<p>
</p><dl><dt><table cellpadding="0" cellspacing="0"><tbody><tr valign="baseline">
  <td><nobr><b><tt id="l2h-2275" xml:id="l2h-2275" class="function">gettempdir</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></tbody></table></dt>
<dd>
Return the directory currently selected to create temporary files in.
If <code>tempdir</code> is not <code>None</code>, this simply returns its contents;
otherwise, the search described above is performed, and the result
returned.
</dd></dl>

<p>
</p><dl><dt><b><tt id="l2h-2276" xml:id="l2h-2276">template</tt></b></dt>
<dd>
<div class="versionnote"><b>Deprecated since release 2.0.</b>
Use <tt class="function">gettempprefix()</tt> instead.</div><p></p>
When set to a value other than <code>None</code>, this variable defines the
prefix of the final component of the filenames returned by
<tt class="function">mktemp()</tt>.  A string of six random letters and digits is
appended to the prefix to make the filename unique.  On Windows,
the default prefix is <span class="file">~T</span>; on all other systems
it is <span class="file">tmp</span>.

<p>
Older versions of this module used to require that <code>template</code> be
set to <code>None</code> after a call to <tt class="function">os.fork()</tt>; this has not
been necessary since version 1.5.2.
</p></dd></dl>

<p>
</p><dl><dt><table cellpadding="0" cellspacing="0"><tbody><tr valign="baseline">
  <td><nobr><b><tt id="l2h-2277" xml:id="l2h-2277" class="function">gettempprefix</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></tbody></table></dt>
<dd>
Return the filename prefix used to create temporary files.  This does
not contain the directory component.  Using this function is preferred
over reading the <var>template</var> variable directly.

<span class="versionnote">New in version 1.5.2.</span>

</dd></dl>

<div class="navigation">
<div class="online-navigation">
<p></p><hr>
<table align="center" cellpadding="0" cellspacing="2" width="100%">
<tbody><tr>
<td class="online-navigation"><a rel="prev" title="11.5.1 the dircmp class" href="dircmp-objects.html"><img src="../icons/previous.png" alt="Previous Page" border="0" height="32" width="32"></a></td>
<td class="online-navigation"><a rel="parent" title="11. file and Directory" href="filesys.html"><img src="../icons/up.png" alt="Up one Level" border="0" height="32" width="32"></a></td>
<td class="online-navigation"><a rel="next" title="11.7 glob  " href="module-glob.html"><img src="../icons/next.png" alt="Next Page" border="0" height="32" width="32"></a></td>
<td align="center" width="100%">Python库参考</td>
<td class="online-navigation"><a rel="contents" title="Table of Contents" href="contents.html"><img src="../icons/contents.png" alt="Contents" border="0" height="32" width="32"></a></td>
<td class="online-navigation"><a href="modindex.html" title="Module Index"><img src="../icons/modules.png" alt="Module Index" border="0" height="32" width="32"></a></td>
<td class="online-navigation"><a rel="index" title="Index" href="genindex.html"><img src="../icons/index.png" alt="Index" border="0" height="32" width="32"></a></td>
</tr></tbody></table>
<div class="online-navigation">
<b class="navlabel">前一节：</b><a class="sectref" rel="prev" href="dircmp-objects.html">11.5.1 dircmp类</a><strong> 上一级：</strong><a class="sectref" rel="parent" href="filesys.html">11.文件和目录</a>
<b class="navlabel">下一节：</b><a class="sectref" rel="next" href="module-glob.html">11.7 glob  </a></div>
</div>
<hr>
<span class="release-info">2.5.1 版， 文档更新在 2007，4，18。翻译：<a href="http://www.zkfarmer.org">zkfarmer</a>。</span></div>
<!--End of Navigation Panel-->
<address>
关于建议修改的信息查阅 <i><a href="about.html">关于这个文档...</a></i>。
</address>
</body></html>